package com.hwbsite.hwbback.utils;

import cn.hutool.core.util.StrUtil;
import com.hwbsite.hwbback.consstants.ResConstant;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DhRes<T>  implements Serializable {
    private static final long serialVersionUID = 7218465808908623780L;

    private Integer code;
    private T data;
    private String msg;

   public static <T> DhRes<T> res(Integer code, T data, String msg){

       DhRes<T> res = new DhRes();
       res.setCode(code);
       res.setData(data);
       res.setMsg(msg);

       return  res;
   }

   public Boolean isSuccess(){
       return ResConstant.SUCCESS_CODE == this.code? true : false;
   }

    public static <T> DhRes<T> success(){
        return res(ResConstant.SUCCESS_CODE, null, ResConstant.map.get(ResConstant.SUCCESS_CODE));
    }

   public static <T> DhRes<T> success(T data){
       return res(ResConstant.SUCCESS_CODE, data, ResConstant.map.get(ResConstant.SUCCESS_CODE));
   }

    public static <T> DhRes<T> fail(Integer code){
        return res(code, null, ResConstant.map.get(code));
    }

    public static <T> DhRes<T> fail(String field , String errorMsg ){ return res(ResConstant.PARAM_ERROR_CODE,null,
            StrUtil.format(ResConstant.map.get(ResConstant.PARAM_ERROR_CODE),field, errorMsg ));
       }

    public static <T> DhRes<T> fail(Integer code, String msg){
        return res(code, null, msg);
    }

    public static <T> DhRes<T> fail(Integer code, T data){
        return res(code, data, ResConstant.map.get(code));
    }

    public static <T> DhRes<T> fail(Integer code, T data, String msg){
        return res(code, data, msg);
    }
}
